/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.approve.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.approve.entity.HomeApprove;
import com.fosung.frame.modules.approve.service.HomeApproveService;
import com.fosung.frame.modules.approve.service.PartyScoreService;
import com.fosung.frame.modules.checkinfos.service.CheckinfosService;
import com.fosung.frame.modules.score.entity.PartyScoreDetail;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * 党组织积分申请管理Controller
 * @author ygl
 * @version 2017-08-30
 */
@Controller
@RequestMapping(value = "${adminPath}/approve/partyApprove")
public class PartyApproveController extends BaseController {

	@Autowired
	private HomeApproveService homeApproveService;
	
	@Autowired
	private CheckinfosService checkinfosService;
	
	@Autowired
	private PartyScoreService partyScoreService;
	
	@ModelAttribute
	public HomeApprove get(@RequestParam(required=false) String id) {
		HomeApprove entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = homeApproveService.get(id);
		}
		if (entity == null){
			entity = new HomeApprove();
		}
		return entity;
	}
	
	@RequiresPermissions("approve:partyApprove:view")
	@RequestMapping(value = {"list", ""})
	public String list(HomeApprove homeApprove, HttpServletRequest request, HttpServletResponse response, Model model) {
		homeApprove.setOrganizationid(UserUtils.getUser().getOffice().getId());
		Page<HomeApprove> page = homeApproveService.findPartyApprovePage(new Page<HomeApprove>(request, response), homeApprove); 
		model.addAttribute("page", page);
		return "modules/approve/partyApproveList";
	}

	@RequiresPermissions("approve:partyApprove:view")
	@RequestMapping(value = "form")
	public String form(HomeApprove homeApprove, Model model) {
		String username = UserUtils.getUser().getName();
		model.addAttribute("username", username);
		
		// 循环获取审批意见，然后赋值
		homeApprove = get(homeApprove.getId());
		homeApprove.setCheckinfos(checkinfosService.findCheckinfosByOne(homeApprove));
		model.addAttribute("homeApprove", homeApprove);
		
		//获取党组织积分明细
		PartyScoreDetail partyScoreDetail = new PartyScoreDetail();
		partyScoreDetail.setOrganizeLifeId(homeApprove.getId());
		partyScoreDetail.setOrganizeId(homeApprove.getOrganizationid());
		partyScoreDetail.setYear(homeApprove.getColumn24());
		List<PartyScoreDetail> partyScoreDetailList = partyScoreService.findPartyScoreDetailList(partyScoreDetail);
		
		Map<Integer, String> yyzlmap = new HashMap<Integer, String>();
		if (homeApprove.getYyzl() != null && !homeApprove.getYyzl().equals("")) {
			String[] yyzl = homeApprove.getYyzl().split("\\|");
			for (int i = 1; i < yyzl.length; i++) {
				yyzlmap.put(i, yyzl[i]);
			}
			model.addAttribute("yyzlmap", yyzlmap);
		}
		model.addAttribute("homeApprove", homeApprove);
		model.addAttribute("partyScoreDetailList", partyScoreDetailList);
		return "modules/approve/partyApproveForm";
	}

	
	@RequiresPermissions("approve:partyApprove:view")
	@RequestMapping(value = "updateApproveState")
	public String updateApproveState(HomeApprove partyApprove, RedirectAttributes redirectAttributes) {
		String[] ids = partyApprove.getIds().split(",");
		try {
			for(int i=0; i<ids.length; i++) {
				partyApprove.setId(ids[i]);
			}
			homeApproveService.updateApproveShState(partyApprove);
			addMessage(redirectAttributes, "操作成功！");
		}catch(Exception e) {
			addMessage(redirectAttributes, "操作失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/approve/partyApprove/?repage";
	}

	
	/**
	 * 批量退回、拒绝党组织积分申请操作
	 * @param homeApprove
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("approve:partyApprove:view")
	@RequestMapping(value = "batchUpdateOrganizeLifeStatus")
	public String batchUpdateOrganizeLifeStatus(HomeApprove homeApprove, RedirectAttributes redirectAttributes) {
		String msg = partyScoreService.batchUpdateOrganizeLifeStatus(homeApprove);
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/approve/partyApprove/?repage";
	}
	
	/**
	 * 批量同意积分申请操作
	 * @param homeApprove
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("approve:activityApprove:view")
	@RequestMapping(value = "batchAgreeOrganizeLife")
	public String batchAgreeOrganizeLife(HomeApprove homeApprove, RedirectAttributes redirectAttributes) {
		String msg = partyScoreService.batchAgreeOrganizeLife(homeApprove);
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/approve/partyApprove/?repage";
	}
}